var SCORE = 0;

$(function () {
    var loading = weui.loading('loading', {
        className: 'custom-classname'
    });
    var orderId = Common.fn.getUrlParam('oid');
    if (!orderId) {
        location.href = 'or_buy_auction.html';
    }
    var result = Model.order.orderDetail({
        orderId: orderId
    });
    if (!sessionStorage.getItem('hmdj_stopError')) {
        sessionStorage.setItem('hmdj_stopError', true);
    } else {
        if (result.code) {
            Common.fn.formatOutput(result);
            loading.hide();
            return false;
        }
    }
    // 设置页面信息
    $('.order-status').text(result.data.order.state);
    $('.order-id').text(result.data.order.orderId);
    $('.order-count').text('￥' + result.data.order.price);
    $('.user-nickname').text(result.data.user.nickanme);
    $('.user-phone').text(result.data.user.phone);
    $('.user-address').text(result.data.user.address);
    $('.portrait-img').attr('src', result.data.goods.picture);
    $('.order-good-title').text(result.data.goods.content);
    $('.goods-price').text(result.data.goods.topPrice);
    $('.toShopBtn').attr('href', 'shop_detail.html?id=' + result.data.businessId);
    switch (result.data.order.state) {
		case '竞拍中':
			$('.apply-refund-btn').css('display', 'none')
			$('.wuliu-order').text('暂无')
			$('.wuliu-copy-btn').css('display', 'none')
			$('.s-footer').css('display', 'none')
			$('.goods-detail').css('cursor', 'pointer')
			$(document).on('click', '.goods-detail', function() {
				location.href = 'goods_detail.html?gid=' + result.data.goods.goodsId + '&bid=' + result.data.businessId
			})
			break
		case '待付款':
			$('.apply-refund-btn').css('display', 'none')
			$('.wuliu-order').text('暂无')
			$('.wuliu-copy-btn').css('display', 'none')
			$('.s-footer').css('display', 'none')
			// 计算付款倒计时
			$('.order-countdown-cell').removeClass('hide')
			var endTime = result.data.endTime,
				endYear = parseInt(endTime.substr(0, 4)),
				endMonth = parseInt(endTime.substr(5, 2)),
				endDay = parseInt(endTime.substr(8, 2)),
				endHour = parseInt(endTime.substr(11, 2)),
				endMinute = parseInt(endTime.substr(14, 2)),
				endSecond = parseInt(endTime.substr(17, 2)),
				targetEndTime = new Date(endYear, endMonth - 1, endDay, endHour, endMinute, endSecond).getTime()
			// 抢购倒计时
			var time = setInterval(function() {
				var currentTime = Date.now(),
					offsetTime = targetEndTime - currentTime
				if (offsetTime < 0) {
					clearInterval(time)
					return false
				}
				var offsetHour = Math.floor(offsetTime / (3600 * 1e3)),
					remainTime = offsetTime - offsetHour * 3600 * 1e3,
					offsetMinute = Math.floor(remainTime / (60 * 1e3)),
					remainTime2 = remainTime - offsetMinute * 60 * 1e3,
					offsetSecond = Math.floor(remainTime2 / 1e3)
				$('.order-countdown').text(offsetHour + '时' + offsetMinute + '分' + offsetSecond + '秒')
				if (offsetHour == 0 && offsetMinute == 0 && offsetSecond == 0) {
					clearInterval(time)
				}
			}, 1000)
			$('.total-price').text('￥' + parseInt(result.data.order.price - 1))
			$('.order-footer-pay').css('display', 'block')
			$('.order-footer-pay-btn').attr('data-id', result.data.order.orderId)
			break
		case '待发货':
			$('.wuliu-order').text('暂无')
			$('.wuliu-copy-btn').css('display', 'none')
			$('.s-footer').css('display', 'none')
			$('.apply-refund-btn').attr('data-id', result.data.order.orderId)
			// 计算付款倒计时
			$('.order-countdown-cell').removeClass('hide')
			$('.order-countdown-cell .weui-cell__hd').text('商家发货倒计时：')
			$('.wait-pay-tip').text('未确认48小时后自动退款给用户')
			var endTime = result.data.endTime,
				endYear = parseInt(endTime.substr(0, 4)),
				endMonth = parseInt(endTime.substr(5, 2)),
				endDay = parseInt(endTime.substr(8, 2)),
				endHour = parseInt(endTime.substr(11, 2)),
				endMinute = parseInt(endTime.substr(14, 2)),
				endSecond = parseInt(endTime.substr(17, 2)),
				targetEndTime = new Date(endYear, endMonth - 1, endDay, endHour, endMinute, endSecond).getTime()
			// 抢购倒计时
			var time = setInterval(function() {
				var currentTime = Date.now(),
					offsetTime = targetEndTime - currentTime
				if (offsetTime < 0) {
					clearInterval(time)
					return false
				}
				var offsetDay = Math.floor(offsetTime / (3600 * 24 * 1e3)),
					remainTime0 = offsetTime - offsetDay * 3600 * 24 * 1e3,
					offsetHour = Math.floor(remainTime0 / (3600 * 1e3)),
					remainTime = remainTime0 - offsetHour * 3600 * 1e3,
					offsetMinute = Math.floor(remainTime / (60 * 1e3)),
					remainTime2 = remainTime - offsetMinute * 60 * 1e3,
					offsetSecond = Math.floor(remainTime2 / 1e3)
				$('.order-countdown').text(offsetDay + '天' + offsetHour + '时' + offsetMinute + '分' + offsetSecond + '秒')
				if (offsetHour == 0 && offsetMinute == 0 && offsetSecond == 0) {
					clearInterval(time)
				}
			}, 1000)
			break
		case '待收货':
			$('.wuliu-order').text(result.data.order.express + ' ' + result.data.order.expressOrder)
			$('.s-footer').css('display', 'none')
			$('.apply-refund-btn').attr('data-id', result.data.order.orderId)
			// 计算付款倒计时
			$('.order-countdown-cell').removeClass('hide')
			$('.order-countdown-cell .weui-cell__hd').text('确认收货倒计时：')
			$('.wait-pay-tip').addClass('hide')
			var endTime = result.data.endTime,
				endYear = parseInt(endTime.substr(0, 4)),
				endMonth = parseInt(endTime.substr(5, 2)),
				endDay = parseInt(endTime.substr(8, 2)),
				endHour = parseInt(endTime.substr(11, 2)),
				endMinute = parseInt(endTime.substr(14, 2)),
				endSecond = parseInt(endTime.substr(17, 2)),
				targetEndTime = new Date(endYear, endMonth - 1, endDay, endHour, endMinute, endSecond).getTime()
			// 抢购倒计时
			var time = setInterval(function() {
				var currentTime = Date.now(),
					offsetTime = targetEndTime - currentTime
				if (offsetTime < 0) {
					clearInterval(time)
					return false
				}
				var offsetDay = Math.floor(offsetTime / (3600 * 24 * 1e3)),
					remainTime0 = offsetTime - offsetDay * 3600 * 24 * 1e3,
					offsetHour = Math.floor(remainTime0 / (3600 * 1e3)),
					remainTime = remainTime0 - offsetHour * 3600 * 1e3,
					offsetMinute = Math.floor(remainTime / (60 * 1e3)),
					remainTime2 = remainTime - offsetMinute * 60 * 1e3,
					offsetSecond = Math.floor(remainTime2 / 1e3)
				$('.order-countdown').text(offsetDay + '天' + offsetHour + '时' + offsetMinute + '分' + offsetSecond + '秒')
				if (offsetHour == 0 && offsetMinute == 0 && offsetSecond == 0) {
					clearInterval(time)
				}
			}, 1000)
			break
		case '已完成':
			$('.apply-refund-btn').css('display', 'none')
			$('.order-footer-evaluate').css('display', 'block')
			$('.wuliu-order').text(result.data.order.express + ' ' + result.data.order.expressOrder)
			// $('.s-footer').css('display', 'none');
			break
		case '退款中':
			$('.wuliu-order').text(result.data.order.express + ' ' + result.data.order.expressOrder)
			$('.apply-refund-btn').css('display', 'none')
			$('.s-footer').css('display', 'none')
			$('.order-footer-payoff').css('display', 'block')
			$('.cancel-footer-return-btn').attr('data-id', result.data.order.orderId)
			// 计算付款倒计时
			$('.order-countdown-cell').removeClass('hide')
			$('.order-countdown-cell .weui-cell__hd').text('退款倒计时：')
			$('.wait-pay-tip').text('未确认48小时后自动退款给用户')
			var endTime = result.data.endTime,
				endYear = parseInt(endTime.substr(0, 4)),
				endMonth = parseInt(endTime.substr(5, 2)),
				endDay = parseInt(endTime.substr(8, 2)),
				endHour = parseInt(endTime.substr(11, 2)),
				endMinute = parseInt(endTime.substr(14, 2)),
				endSecond = parseInt(endTime.substr(17, 2)),
				targetEndTime = new Date(endYear, endMonth - 1, endDay, endHour, endMinute, endSecond).getTime()
			// 抢购倒计时
			var time = setInterval(function() {
				var currentTime = Date.now(),
					offsetTime = targetEndTime - currentTime
				if (offsetTime < 0) {
					clearInterval(time)
					return false
				}
				var offsetDay = Math.floor(offsetTime / (3600 * 24 * 1e3)),
					remainTime0 = offsetTime - offsetDay * 3600 * 24 * 1e3,
					offsetHour = Math.floor(remainTime0 / (3600 * 1e3)),
					remainTime = remainTime0 - offsetHour * 3600 * 1e3,
					offsetMinute = Math.floor(remainTime / (60 * 1e3)),
					remainTime2 = remainTime - offsetMinute * 60 * 1e3,
					offsetSecond = Math.floor(remainTime2 / 1e3)
				$('.order-countdown').text(offsetDay + '天' + offsetHour + '时' + offsetMinute + '分' + offsetSecond + '秒')
				if (offsetHour == 0 && offsetMinute == 0 && offsetSecond == 0) {
					clearInterval(time)
				}
			}, 1000)
			break
		default:
			$('.apply-refund-btn').css('display', 'none')
			$('.wuliu-order').text('暂无')
			$('.wuliu-copy-btn').css('display', 'none')
			$('.s-footer').css('display', 'none')
			break
	}
    // 判断如果已star，则隐藏底部栏
    var isStar = result.data.order.star;
    if(isStar != null) {
        $('.s-footer').css('display', 'none');
        $('.user-evaluate').removeClass('hide');
        for(var i = 0;i < isStar;i++) {
            var item = '<img src="../img/icon/star1.png" style="width: 1.5rem; height: 1.5rem;" />';
            $('.user-star').append(item);
        }
    }else {
        
    }
    loading.hide();
});

// 复制订单编号
$('#copy-orderId-btn').on('click', function () {
    var orderId = $('.order-id').text();
    var clipboard = new Clipboard("#copy-orderId-btn", {
        text: function text() {
            return orderId;
        }
    });
    clipboard.on('success', function (e) {
        weui.toast('已成功复制到剪贴板', {
            duration: 2000,
            callback: function callback() {}
        });
    });
    clipboard.on('error', function (e) {
        weui.topTips('复制剪贴板失败，请手动复制');
    });
});

// 复制快递信息
$('#wuliu-copy-btn').on('click', function () {
    var wuliu = $('.wuliu-order').text();
    var clipboard = new Clipboard("#wuliu-copy-btn", {
        text: function text() {
            return wuliu;
        }
    });
    clipboard.on('success', function (e) {
        weui.toast('已成功复制到剪贴板', {
            duration: 2000,
            callback: function callback() {}
        });
    });
    clipboard.on('error', function (e) {
        weui.topTips('复制剪贴板失败，请手动复制');
    });
});

// 去支付按钮
$(document).on('click', '.order-footer-pay-btn', function () {
    var orderId = $(this).attr('data-id');
    Common.fn.payForOrder(orderId);
});

// 申请退款
$(document).on('click', '.apply-refund-btn', function () {
    var orderId = $(this).attr('data-id');
    Common.fn.backForOrder(orderId);
});

// 取消退款
$(document).on('click', '.cancel-footer-return-btn', function () {
    var orderId = $(this).attr('data-id');
    Common.fn.cancelForReturn(orderId);
});

// 评价商家
$(document).on('click', '.user-footer-star-btn', function() {
    $('.shade').show();
    $('.star-block').show();
});

// 隐藏评价商家
$(document).on('click', '.shade', function() {
    $('.shade').hide();
    $('.star-block').hide();
});

// 点击星星评分
$(document).on('click', '.stars a', function() {
    var store = $(this).attr('data-score');
    for (var i = 0; i < 5; i++) {
		$('.stars img')
			.eq(i)
			.attr('src', '../img/icon/star0.png');
	}
    for(var i = 0;i < store;i++) {
        $('.stars img')
			.eq(i)
			.attr('src', '../img/icon/star1.png');
    }
    SCORE = store;
});

// 确认打分
$(document).on('click', '.submit-star', function() {
    if(SCORE == 0) {
        weui.alert('请点击星星进行打分');
        return false;
    }
    var result = Model.order.evaluate({
        orderId: Common.fn.getUrlParam('oid'),
        star: SCORE
    });
    if (result.code) {
		Common.fn.formatOutput(result);
		loading.hide();
		return false;
    }
    weui.alert('评价成功！', function() {
        location.reload();
    });
});